<?php
if (!(defined('IN_IA'))) 
{
	exit('Access Denied');
}

//服务类订单
class Index_MxShopPage extends MobilePage {

    private $sOrderTable = '';
    private $sGoodsTable = '';
    private $sGoodsPriceTable = '';
    private $mendianTable = '';
    private $status = array();
    private $member = array();

    public function __construct()
    {
        global $_W;
        parent::__construct();

        $this->sOrderTable = tablename('mx_shop_service_order');
        $this->sGoodsTable = tablename('mx_shop_service_goods');
        $this->sGoodsPriceTable = tablename('mx_shop_service_goods_type_price');
        $this->mendianTable = tablename('mx_shop_mendian');

        //获取用户信息
        $this->member = m('userinfo')->getUser($_W['openid']);

        $this->status = array
        (
            1 => array('css' => 'danger', 'name' => '待付款'),
            2 => array('css' => 'warning', 'name' => '进行中'),
            3 => array('css' => 'success', 'name' => '已完成'),
            4 => array('css' => 'default', 'name' => '已关闭'),
            5 => array('css' => 'danger', 'name' => '维权申请'),
            6 => array('css' => 'default', 'name' => '维权完成',)
        );
    }

    //根据设备号选择价格套餐页面
	public function main()
    {
		global $_W, $_GPC;

        //获取令牌
        $token = $this->getToken();

		include $this->template();
	}

    //生成订单接口
    public function createSOrder()
    {
        global $_W, $_GPC;

        //验证令牌
        if(empty($_GPC['token']) || !$this->checkToken($_GPC['token']))
        {
            show_json(0, '参数有误');
        }

        $number = intval($_GPC['number']);
        $priceid = intval($_GPC['priceid']);
        if(empty($number) || empty($priceid)){
            show_json(0, '参数错误');
        }

        $openid = $_W['openid'];
        $uniacid = $_W['uniacid'];

        //重复下单
        $open_redis = function_exists('redis') && !(is_error(redis()));
        if ($open_redis)
        {
            $redis_key = $_W['uniacid'] . '_sorder_submit_' . $openid;
            $redis = redis();
            if (!(is_error($redis)))
            {
                if ($redis->setnx($redis_key, time()))
                {
                    $redis->expireAt($redis_key, time() + 2);
                }
                else
                {
                    show_json(0, '不要短时间重复下单!');
                }
            }
        }

        //获取用户信息
        $member = $this->member;
        if ($member['isblack'] == 1)
        {
            show_json(0, '该用户禁止访问');
        }

        //获取设备详情
        $sgoods = m('sgoods')->getDetail($number);
        if(empty($sgoods))
        {
            show_json(0, '该设备不存在！');
        }
        if($sgoods['isnormal'] == 2)
        {
            show_json(0, '该设备已有人使用！');
        }
        if($sgoods['isnormal'] == 0)
        {
            show_json(0, '该设备正在维修中！');
        }

        //获取门店详情
        $store = m('sorder')->getDetailToTableById($this->mendianTable, $sgoods['mendian_id'], array('mendianname', 'mdnumber'));
        //获取价格套餐
        $typeprice = m('sorder')->getDetailToTableById($this->sGoodsPriceTable, $priceid);

        $realprice = $typeprice['price'];

        //是否使用积分抵扣
//        $deductcredit = 0;
//        if($isscore == 1)
//        {
//            if(!empty($typeprice['most_reduce_price']))
//            {
//                $credits = m('userinfo')->getCredit($member['id'], 'credit1');
//                if(intval($credits) <= intval($typeprice['most_reduce_price']))
//                {
//                    $deductcredit = intval($credits);
//                }
//                else
//                {
//                    $deductcredit = intval($typeprice['most_reduce_price']);
//                }
//
//                if($deductcredit > 0 && ($realprice - $deductcredit)>=0)
//                {
//                    m('userinfo')->setCredit($member['id'], 'credit1', -intval($deductcredit), array($_W['member']['uid'], $sgoods['name'] . '消费金额抵扣积分' . $deductcredit));
//                }
//            }
//        }
//        $realprice = ($realprice - $deductcredit)>=0 ? ($realprice - $deductcredit) : $realprice;

        $ordersn = m('common')->createNO('service_order', 'ordersn', 'SOD');

        $order = array(
            'uniacid' => $uniacid,
            'uid' => $member['uid'],
            'ordersn' => $ordersn,
            'goods_id' => $sgoods['id'],
            'couponid' => 0,
            'couponcredit' => 0,
            'price_id' => $priceid,
            'goods_name' => $sgoods['name'],
            'thumb' => $sgoods['thumb'],
            'price' => $typeprice['price'],
            'realprice' => $realprice,
            'goodsprice' => $typeprice['price'],
            'marketprice' => $typeprice['marketprice'],
            'mendian_id' => $store['id'],
            'mendian_name' => $store['mendianname'],
            'status' => 1,
            'createtime' => time()
        );

        pdo_insert('mx_shop_service_order', $order);
        $url = mobileUrl('spay', array('ordersn'=>$ordersn));
        show_json(1, array('url' => $url));
    }

    //我的订单列表
    public function mylist()
    {
        global $_W, $_GPC;

        //获取令牌
        $token = $this->getToken();

        include $this->template();
    }

    //我的订单列表
    public function mylist2()
    {
        global $_W, $_GPC;

        //获取令牌
        $token = $this->getToken();

        include $this->template();
    }

    //订单数据接口
    public function getList()
    {
        global $_W, $_GPC;

        //验证令牌
        if (empty($_GPC['token']) || !$this->checkToken($_GPC['token']))
        {
            show_json(0, '参数有误');
        }


        $data = m('sorder')->getMyList();

        $list = $data['list'];
        $total = $data['total'];
        $pagesize = $data['pagesize'];
        unset($data);

        $res = array();
        if (!empty($list) && is_array($list))
        {
            foreach ($list as $index=>$item)
            {
                //获取设备详情
                $sgoods = m('sorder')->getDetailToTableById($this->sGoodsTable, $item['goods_id'], array('name', 'docking_number', 'thumb'));
                //获取门店详情
                $store = m('sorder')->getDetailToTableById($this->mendianTable, $item['mendian_id'], array('mendianname', 'mdnumber'));

                $res[$index]['ordersn'] = $item['ordersn'];
                $res[$index]['status'] = $this->status[$item['status']]['name'];

                $res[$index]['price'] = $item['price'];
                $res[$index]['onPrice'] = $item['realprice'];
                $res[$index]['quantity'] = 1;

                $res[$index]['storeName'] = $store['mendianname'];
                $res[$index]['storeNumber'] = $store['mdnumber'];

                $res[$index]['goodsName'] = $sgoods['name'];
                $res[$index]['goodsLogo'] = tomedia($sgoods['thumb']);

                $res[$index]['addDate'] = date('Y-m-d H:i:s', $item['createtime']);
            }
        }

        show_json(1, array('total'=>$total, 'pagesize'=>$pagesize, 'data'=>$res));

    }

    //订单详情
    public function detail()
    {
        global $_W, $_GPC;

        //获取令牌
        $token = $this->getToken();

        include $this->template();
    }

    //订单详细信息接口
    public function sOrderInfo()
    {
        global $_W, $_GPC;

        //验证令牌
        if(empty($_GPC['token']) || !$this->checkToken($_GPC['token']))
        {
            show_json(0, '参数有误');
        }

        $sorder = m('sorder')->getDetailByOrdersn($_GPC['ordersn']);
        if(empty($sorder))
        {
            show_json(0, '订单不存在');
        }

        //获取设备详情
        $sgoods = m('sorder')->getDetailToTableById($this->sGoodsTable, $sorder['goods_id'], array('name', 'docking_number', 'thumb'));
        //获取门店详情
        $store = m('sorder')->getDetailToTableById($this->mendianTable, $sgoods['mendian_id'], array('mendianname', 'mdnumber'));
        //获取评价
        $comment = m('sorder')->getCommentDetailByOrderId($sorder['id']);
        $images = unserialize($comment['images']);
        $imgs = array();
        if(!empty($images))
        {
            foreach ($images as $item)
            {
                $imgs[] = tomedia($item);
            }

        }

        $res = array(
            'ordersn' => $sorder['ordersn'],
            'price' => $sorder['price'],
            'isScore' => $sorder['deductprice'],
            'onPrice' => $sorder['realprice'],
            'storeName' => $store['mendianname'],
            'storeNumber' => $store['mdnumber'],
            'goodsName' => $sgoods['name'],
            'goodsNumber' => $sgoods['docking_number'],
            'goodsLogo' => $sgoods['thumb'],
            'quantity' => 1,
            'addDate' => date('Y-m-d H:i:s', $sorder['createtime']),
            'payDate' => date('Y-m-d H:i:s', $sorder['createtime']),
            'finishDate' => date('Y-m-d H:i:s', $sorder['finishtime']),
            'status' => $this->status[$sorder['status']]['name'],
            'level' => $comment['level'],
            'content' => $comment['content'],
            'images' => $imgs,
        );

        show_json(1, array('data'=>$res));
    }



    //取消订单接口
    public function cancel()
    {
        global $_W, $_GPC;

        //验证令牌
        if(empty($_GPC['token']) || !$this->checkToken($_GPC['token']))
        {
            show_json(0, '参数有误');
        }

        //获取用户信息
        $member = $this->member;

        $uniacid = $_W['uniacid'];

        $ordersn = intval($_GPC['ordersn']);
        $cont = $_GPC['cont'];
        if(empty($ordersn) || empty($cont))
        {
            show_json(0, '参数有误！');
        }

        $order = m('sorder')->getDetailByOrdersn($ordersn);
        if(empty($order) || $order['status'] != 1)
        {
            show_json(0, '订单不存在！');
        }

        $updata = array
        (
            'status' => 4,
            'close_desc' => $cont,
            'deductcredit' => 0,
            'deductprice' => 0,
            'canceltime' => time(),
        );

        pdo_update('mx_shop_service_order', $updata, array('id'=>$order['id'], 'uid'=>$member['id']));

        //退积分
        if($order['deductcredit']> 0){
            m('userinfo')->setCredit($member['id'], 'credit1', intval($order['deductcredit']), array($_W['member']['uid'], $order['goods_name'] . '取消订单退回彩融积分' . $order['deductcredit']));
        }

        show_json(1);
    }

    //根据订单号回收订单
    public function reclaim()
    {
        global $_W, $_GPC;

        //验证令牌
        if(empty($_GPC['token']) || !$this->checkToken($_GPC['token']))
        {
            show_json(0, '参数有误');
        }

        $ordersn = intval($_GPC['ordersn']);
        if(empty($ordersn))
        {
            show_json(0, '参数有误！');
        }

        $order = m('sorder')->getDetailByOrdersn($ordersn);
        if(empty($order) || !in_array($order['status'], array(3,4,6)) || $order['isrecycle'] == 1){
            show_json(0, '订单不存在！');
        }

        //获取用户信息
        $member = $this->member;

        $updata = array(
            'isrecycle' => 1,
            'recycletime' => time(),
        );
        pdo_update('mx_shop_service_order', $updata, array('id'=>$order['id'], 'uid'=>$member['id']));

        show_json(1, array('code'=>'666'));
    }

    //根据订单号恢复订单
    public function backReclaim()
    {
        global $_W, $_GPC;

        //验证令牌
        if(empty($_GPC['token']) || !$this->checkToken($_GPC['token']))
        {
            show_json(0, '参数有误');
        }

        $ordersn = intval($_GPC['ordersn']);
        if(empty($ordersn))
        {
            show_json(0, '参数有误！');
        }

        $order = m('sorder')->getDetailByOrdersn($ordersn);
        if(empty($order) || !in_array($order['status'], array(3,4,6)) || $order['isrecycle'] == 0){
            show_json(0, '订单不存在！');
        }

        //获取用户信息
        $member = $this->member;

        $updata = array(
            'isrecycle' => 0,
            'restoretime' => time(),
        );
        pdo_update('mx_shop_service_order', $updata, array('id'=>$order['id'], 'uid'=>$member['id']));

        show_json(1, array('code'=>'666'));
    }

    //根据订单号删除订单
    public function delete()
    {
        global $_W, $_GPC;

        //验证令牌
        if(empty($_GPC['token']) || !$this->checkToken($_GPC['token'])){
            show_json(0, '参数有误');
        }

        $ordersn = intval($_GPC['ordersn']);
        if(empty($ordersn))
        {
            show_json(0, '参数有误！');
        }

        $order = m('sorder')->getDetailByOrdersn($ordersn);
        if(empty($order) || $order['isrecycle'] == 0){
            show_json(0, '订单不存在！');
        }

        //获取用户信息
        $member = $this->member;

        $updata = array(
            'userdeleted' => 1,
            'userdeletetime' => time(),
        );
        pdo_update('mx_shop_service_order', $updata, array('id'=>$order['id'], 'uid'=>$member['id']));


        show_json(1, array('code'=>'666'));
    }


}
